import java.util.ArrayList;
import java.util.List;

class Solution2 {
    private int target;
    private List<Integer> path;
    private List<List<Integer>> ret;
    public List<List<Integer>> combinationSum(int[] candidates, int _target) {
        target = _target;
        path = new ArrayList<>();
        ret = new ArrayList<>();
        dfs(candidates, 0, 0);
        return ret;
    }
    private void dfs(int[] candidates, int sum, int index) {
        if(sum == target) {
            ret.add(new ArrayList<>(path));
            return;
        }
        if(sum > target) {
            return;
        }
        for(int i = index; i < candidates.length; i++) {
            path.add(candidates[i]);
            dfs(candidates, sum + candidates[i], i);
            path.remove(path.size()-1);
        }
    }
}